package com.loan.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.loan.common.utils.DateUtils;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.OperatorLog;
import com.loan.system.domain.req.OperatorReq;
import com.loan.system.mapper.OperatorLogMapper;
import com.loan.system.service.OperatorLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shiguang
 * @date 2022年09月17日 18:47
 */
@Service
@RequiredArgsConstructor
public class OperatorLogServiceImpl implements OperatorLogService {

    private final OperatorLogMapper operatorLogMapper;

    @Override
    public List<OperatorLog> list() {
        return operatorLogMapper.selectList(new QueryWrapper<OperatorLog>().eq("status","0").lt("create_time", DateUtils.addMinutes(new Date(),-2)));
    }

    @Override
    public void updateById(OperatorLog entity) {
        operatorLogMapper.updateById(entity);
    }

    @Override
    public Object getUserOperatorLog(Long userId) {
        OperatorLog operatorLog = operatorLogMapper.selectOne(new QueryWrapper<OperatorLog>().eq("user_id", userId).orderByDesc("id").last("limit 1"));
        if (ObjectUtil.isNotNull(operatorLog) && operatorLog.getStatus().equals("2") && StringUtils.isNotBlank(operatorLog.getResult())){
            OperatorReq operatorReq = JSONObject.parseObject(operatorLog.getResult(), OperatorReq.class);
            return operatorReq;
        }
        return null;
    }
}
